﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes.actions {
    import Classes.*;
    import Classes.util.*;
    import com.greensock.*;

    public class ActionPickup extends NPCAction {

        public static const STATE_APPEAR:int = 0;
        public static const STATE_TRAVEL:int = 1;
        public static const STATE_PICKUP:int = 2;
        public static const STATE_DISAPPEAR:int = 3;
        public static const NUM_STATES:int = 4;

        protected var m_state:int;
        protected var m_nextState:int;
        protected var m_target:MapResource;
        protected var m_firstTarget:Boolean;

        public function ActionPickup(_arg1:NPC, _arg2:int, _arg3:MapResource){
            super(_arg1);
            this.m_target = _arg3;
            this.m_state = _arg2;
            this.m_nextState = ((_arg2 + 1) % NUM_STATES);
            this.m_firstTarget = (_arg3 == null);
        }
        override public function update(_arg1:Number):void{
            super.update(_arg1);
            this.branchToState(this.m_state);
            m_npc.getStateMachine().removeState(this);
            if (this.m_state != STATE_DISAPPEAR){
                Global.world.citySim.pickupManager.addPickupAction(m_npc, this.m_nextState, this.m_target);
            };
        }
        protected function getNextState():int{
            return (this.m_nextState);
        }
        protected function setNextState(_arg1:int):void{
            this.m_nextState = _arg1;
        }
        protected function getTarget():MapResource{
            return (this.m_target);
        }
        protected function isFirstTarget():Boolean{
            return (this.m_firstTarget);
        }
        protected function branchToState(_arg1:int):void{
            switch (_arg1){
                case STATE_APPEAR:
                    this.onAppear();
                    break;
                case STATE_TRAVEL:
                    this.onTravel();
                    break;
                case STATE_PICKUP:
                    this.onPickup();
                    break;
                case STATE_DISAPPEAR:
                    this.onDisappear();
                    break;
            };
        }
        protected function onAppear():void{
            this.setNextState(STATE_TRAVEL);
        }
        protected function onTravel():void{
            this.m_target = Global.world.citySim.pickupManager.dequeue(m_npc.getItemName());
            if (!this.m_target){
                this.setNextState(STATE_DISAPPEAR);
                return;
            };
            this.setNextState(STATE_PICKUP);
        }
        protected function onPickup():void{
            if (this.getTarget()){
                m_npc.getStateMachine().addActions(new ActionFn(m_npc, function ():void{
                    m_npc.setPosition(getTarget().getHotspot().x, getTarget().getHotspot().y);
                    m_npc.conditionallyReattach();
                    getTarget().unlock();
                }));
            };
            this.setNextState(STATE_TRAVEL);
        }
        protected function onDisappear():void{
            TweenLite.to(m_npc, 0.3, {alpha:0});
            TimerUtil.callLater(function ():void{
                if (Global.world.citySim.pickupManager.peek(m_npc.getItemName())){
                    Global.world.citySim.pickupManager.addPickupAction(m_npc, STATE_APPEAR, null, false);
                } else {
                    Global.world.citySim.npcManager.removePickupNPC(m_npc.getItemName());
                };
            }, 300);
        }
        public function isDisappearing():Boolean{
            return ((this.m_state == STATE_DISAPPEAR));
        }

    }
}//package Classes.actions 
